package es.udc.fic.hamNeggs.web;

import org.apache.log4j.Logger;

import es.udc.fic.hamNeggs.dao.InputMeeting;
import es.udc.fic.hamNeggs.manager.MeetingManager;
import es.udc.fic.hamNeggs.manager.ProjectManager;
import es.udc.fic.hamNeggs.manager.UserManager;
import es.udc.fic.hamNeggs.pojo.Meeting;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Servlet implementation class MeetingManagement
 */
public class MeetingManagement extends HttpServlet {
    private static final long   serialVersionUID = 1L;
    private final static Logger LOG              = Logger.getLogger(MeetingManagement.class.getName());

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MeetingManagement() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        LOG.debug("MeetingManagement.doGet in");
        final HttpSession session = request.getSession(true);

        final Long myUser = (Long) session.getAttribute("user");

        final Long myProject = (Long) session.getAttribute("project");

        if (myProject == null) {
            response.getWriter().println("error");
        }

        final Set<InputMeeting> allMeetings = new HashSet<InputMeeting>();

        final UserManager usrmng = new UserManager();

        for (final Meeting meeting : usrmng.getUser(myUser).getGoesToMeetings()) {
            final InputMeeting inputMeeting = new InputMeeting();
            inputMeeting.setMeeting(meeting);
            allMeetings.add(inputMeeting);
        }

        final ArrayList<InputMeeting> array = new ArrayList<InputMeeting>();

        for (final InputMeeting meeting : allMeetings) {
            if (meeting.getProject().equalsIgnoreCase(myProject.toString()))
                array.add(meeting);
        }

        request.setAttribute("meetings", array);

        usrmng.close();

        request.getRequestDispatcher("/meetings.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        final HttpSession session = request.getSession(true);

        final Long myUser = (Long) session.getAttribute("user");
        final Long myProject = (Long) session.getAttribute("project");

        final MeetingManager mtgmng = new MeetingManager();
        final UserManager usrmng = new UserManager();
        final ProjectManager prjmng = new ProjectManager();

        try {
            switch (request.getParameter("action")) {
                case "Delete":
                    final Meeting oldMeeting = mtgmng.getMeeting(new Long(request.getParameter("sprid")));
                    mtgmng.deleteMeeting(oldMeeting);
                    break;
                default:
                    break;

            }
        } catch (Exception e) {
            String error = "[";
            final Enumeration<String> names = request.getParameterNames();
            while (names.hasMoreElements()) {
                final String paramName = names.nextElement();
                final String paramValue = request.getParameter(paramName);
                error = error.concat(paramName + ":" + paramValue + ";");
            }
            error = error.concat("]");
            LOG.error("Error while executing action: " + request.getParameter("action")
                    + "with params: " + error + "\n"
                    , e);
        }

        usrmng.close();
        mtgmng.close();
        prjmng.close();

        final Map<String, String[]> params = request.getParameterMap();
        params.clear();

        doGet(request, response);

    }
}
